﻿using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace XUProtobuf
{
    namespace Editor
    {
        internal class XUProtobufConverterSetting
        {
            public static XUProtobufConverterSetting LoadOrCreate()
            {
                XUProtobufConverterSetting instance;

                string jsonPath = (new DirectoryInfo(Application.dataPath).Parent.FullName) + "/Packages/xuprotobuf.json";
                if (File.Exists(jsonPath))
                {
                    string jsonText = File.ReadAllText(jsonPath, new UTF8Encoding(false));
                    instance = JsonUtility.FromJson<XUProtobufConverterSetting>(jsonText);
                }
                else
                {
                    instance = new XUProtobufConverterSetting();
                    instance.Save();
                }

                return instance;
            }

            public void Save()
            {
                string jsonPath = (new DirectoryInfo(Application.dataPath).Parent.FullName) + "/Packages/xuprotobuf.json";
                string jsonText = JsonUtility.ToJson(this, true);
                if (File.Exists(jsonPath))
                {
                    string lastText = File.ReadAllText(jsonPath, new UTF8Encoding(false));
                    if (!jsonText.Equals(lastText))
                    {
                        File.WriteAllText(jsonPath, jsonText, new UTF8Encoding(false));
                    }
                }
                else
                {
                    if (!Directory.Exists(Path.GetDirectoryName(jsonPath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(jsonPath));
                    }
                    File.WriteAllText(jsonPath, jsonText, new UTF8Encoding(false));
                }
            }

            //=============================================================================================================

            public string protoFolder = "";
            public string csFolder = "";

            public void OnGUI()
            {
                GUILayout.Space(50);

                GUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("Proto文件夹:", GUILayout.Width(80));
                    EditorGUI.BeginDisabledGroup(true);
                    EditorGUILayout.TextField(protoFolder);
                    EditorGUI.EndDisabledGroup();
                    if (GUILayout.Button("...", GUILayout.Width(50)))
                    {
                        string root = Application.dataPath.Replace("Assets", "");
                        string dir = EditorUtility.OpenFolderPanel("选择Proto文件夹", root, "");
                        if (!string.IsNullOrEmpty(dir))
                        {
                            if (dir.StartsWith(root))
                            {
                                protoFolder = "./" + dir.Replace(root, "");
                            }
                            else
                            {
                                EditorUtility.DisplayDialog("提示", "不能选择工程以外的路径", "确定");
                            }
                        }
                    }
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("CS文件夹:", GUILayout.Width(80));
                    EditorGUI.BeginDisabledGroup(true);
                    EditorGUILayout.TextField(csFolder);
                    EditorGUI.EndDisabledGroup();
                    if (GUILayout.Button("...", GUILayout.Width(50)))
                    {
                        string root = Application.dataPath.Replace("Assets", "");
                        string dir = EditorUtility.OpenFolderPanel("选择CS文件夹", root, "");
                        if (!string.IsNullOrEmpty(dir))
                        {
                            if (dir.StartsWith(root))
                            {
                                csFolder = "./" + dir.Replace(root, "");
                            }
                            else
                            {
                                EditorUtility.DisplayDialog("提示", "不能选择工程以外的路径", "确定");
                            }
                        }
                    }
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("导出", GUILayout.Width(200), GUILayout.Height(40)))
                {
                    XUProtobufConverter.Convert();
                }
                GUILayout.EndHorizontal();
            }
        }
    }
}